//
//  MvViewController.m
//  海角音乐
//
//  Created by dllo on 15/8/17.
//  Copyright (c) 2015年 杨元石. All rights reserved.
//

#import "MvViewController.h"

@interface MvViewController ()

@end

@implementation MvViewController

- (void)dealloc
{
    [_dataArr release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    CGFloat title_width = SCREEN_WIDTH*0.533333; // 200
    CGFloat title_height = SCREEN_WIDTH*0.08; // 30
    self.TitleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, title_width, title_height)];
    self.TitleView.textColor = [UIColor whiteColor];
    self.TitleView.textAlignment = NSTextAlignmentCenter;
    self.TitleView.text = @"MV";
    self.navigationItem.titleView = self.TitleView;
    
    
    
    
    
    self.dataArr = [[NSMutableArray alloc] init];
    
    _tabelView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    _tabelView.dataSource = self;
    _tabelView.delegate = self;
    [self.view addSubview:_tabelView];
    [_tabelView release];
    
    HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    HUD.removeFromSuperViewOnHide = YES;
    HUD.labelText = @"loading...";
     _tabelView.separatorColor = [UIColor clearColor];
    [self.view addSubview:HUD];
    HUD.mode = MBProgressHUDModeIndeterminate;
    
    [self getDataForMv:1];
    self.Nextpage = 2;
    
    [_tabelView addFooterWithCallback:^{
        
        [_dataArr removeAllObjects];
        [_tabelView reloadData];
        [self getDataForMv:self.Nextpage];
        // 2.2秒后刷新表格UI
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 刷新表格
            // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
            [_tabelView footerEndRefreshing];
        });
    }];
    
    
    [_tabelView addHeaderWithCallback:^{
        
        [_dataArr removeAllObjects];
        [_tabelView reloadData];
        [self getDataForMv:1];
        // 2.2秒后刷新表格UI
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 刷新表格
            // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
            [_tabelView headerEndRefreshing];
        });
    }];
    
}


#pragma mark --上拉加载更多
- (void)addHeader
{
    __block  MvViewController *singerVc = self;
    [_tabelView addHeaderWithCallback:^{
        
        [singerVc getDataForMv:singerVc.Nextpage];
        singerVc.Nextpage++;
    }];
}


- (void)getDataForMv:(NSInteger)page
{
    NSString *one_string = @"http://api.dongting.com/channel/channel/mvs?page=";
    NSString *url_string = [NSString stringWithFormat:@"%@%ld", one_string, page];
    
    [HttpResponse urlString:url_string getResponseObject:^(id responseObject) {
       
        NSArray *dataArr = [responseObject objectForKey:@"data"];
        for (NSDictionary *dic in dataArr) {
            MvModel *mvModel = [[MvModel alloc] init];
            [mvModel setValuesForKeysWithDictionary:dic];
            [self.dataArr addObject:mvModel];
            [mvModel release];
        }
        HUD.hidden = YES;
        [_tabelView reloadData];        
    }];
    
    
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dataArr count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"cell";
    
    MvTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    if (cell == NO) {
        
        cell = [[MvTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    
    if (self.dataArr.count > indexPath.row) {
        
        MvModel *mvModel = self.dataArr[indexPath.row];
        
        [cell getMvModel:mvModel];
        
    }
    // cell 取消选中颜色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 260*ADJUSTWIDTH;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MVDetailViewController *mvDetailVc = [[[MVDetailViewController alloc] init] autorelease];
    
    MvModel *modelOfmV = [_dataArr objectAtIndex:indexPath.row];
    
    mvDetailVc.mvModel = modelOfmV;
    
    [self.navigationController pushViewController:mvDetailVc animated:YES];
 
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
